// 白色导航框中的导航按下加粗显示
function bold() {
    var bold = document.querySelectorAll('#bold');
    for (var i = 0; i < bold.length; i++ ){
        bold[i].onclick = function() {
            for(var i = 0; i < bold.length; i++) {
                bold[i].className = '';
                bold[i].style.fontWeight = '400';
            }
            this.className = 'onclick_bold';
            this.style.fontWeight = '700';
        }
    }
}
bold();

// 倒计时
function count() {
    var rushs = document.querySelectorAll('.purchase');
    var times = document.querySelectorAll('.times');
    var inputTime = +new Date('2021-8-1 23:30:00');
    countDown();
    setInterval(countDown, 1000);

    function countDown() {
        var nowTime = +new Date();
        var time = (inputTime - nowTime) / 1000;
        var h = parseInt(time / 60 / 60 % 24 );
        if (h < 0 || h == 0) {
            h = '00';
        }
        h = h < 10 && h > 0? '0' + h : h;
        var m = parseInt(time / 60 % 60);
        if (m < 0 || m == 0) {
            m = '00';
        }
        m = m < 10 && m > 0? '0' + m : m;
        var s = parseInt(time % 60 );
        if (s < 0 || s == 0) {
            s = '00';
        }
        s = s < 10 && m > 0? '0' + s : s;
        for (var i = 0; i < times.length; i++){
            times[i].innerHTML = h + ':' + m + ':' + s ; 
            if (h == '00' && m == '00' && s == '00'){
                rushs[i].innerHTML = '已超时';
            }
        }
    }
}
count();

// 专栏获取课程
function get_course() {
     var co_class = document.querySelectorAll('.co_class')[0];
     var lis = co_class.children;
     var courses = document.getElementById('courses');
     var lists = courses.children;
     var all = document.getElementById('all');
     all.style.className = 'classify';
    //  初始状态：显示全部的课程
     function get(){
        axios.get('http://120.24.80.83:3000/course/courseList',{
            params:{
                "classifyId":document.getElementById("classify").value,
            }
        }
        )
            .then(function (res) {
                console.log(res.data);
                var courses = document.getElementById('courses');                   
                for (j = 0;j < res.data.data.length; j++){
                    // 大盒子
                    var course = document.createElement('div');
                    courses.appendChild(course);
                    course.setAttribute('class', 'co_course');
                    // 图片
                    var img = document.createElement('img');
                    course.appendChild(img);
                    img.src = res.data.data[j]['image'];
                    img.setAttribute('class', 'img');
                    // 右边盒子
                    var container = document.createElement('div');
                    course.appendChild(container);
                    container.setAttribute('class', 'co_container');
                    // 标题
                    var title = document.createElement('div');
                    container.appendChild(title);
                    title.innerHTML = res.data.data[j]['title'];
                    title.setAttribute('class', 'co_title');
                    // 简介
                    var brief = document.createElement('div');
                    container.appendChild(brief);
                    brief.innerHTML = res.data.data[j]['brief'];
                    brief.setAttribute('class','co_brief');
                    // 教师
                    var teacher = document.createElement('div');
                    container.appendChild(teacher);
                    teacher.innerHTML = res.data.data[j]['teacherName'] + ' ' + res.data.data[j]['teacherTitle']
                    teacher.setAttribute('class','co_teacher');
                    // 底部盒子
                    var btm = document.createElement('div');
                    container.appendChild(btm);
                    btm.setAttribute('class','co_btm');
                    // 价格
                    var price = document.createElement('div');
                    btm.appendChild(price);
                    price.innerHTML = '￥' + res.data.data[j]['price'];
                    price.setAttribute('class','co_price');
                    // 原价
                    var preprice = document.createElement('div');
                    btm.appendChild(preprice);
                    if (res.data.data[j]['originalPrice']){
                        preprice.innerHTML = '￥' + res.data.data[j]['originalPrice'];
                        preprice.setAttribute('class','co_preprice');}
                    // 人数
                    var number = document.createElement('div');
                    btm.appendChild(number);
                    number.innerHTML = res.data.data[j]['pruchasedCount']; 
                    number.setAttribute('class','co_number');   
                } 
                  
            })
            .catch(function (error) {
                console.log(error)
            })
    }
    get();
    // 绑定点击事件
     for (var i = 0; i < lis.length; i++){
         lis[i].setAttribute('index', i);
         lis[i].onclick = function() {
            // 标签栏变化
             for (var i = 0; i < lis.length;i++){
                 lis[i].className = '';
                 lis[i].id = '';
             }
             this.className = 'current';
            this.id = 'classify';  
            // 显示和隐藏
             var index = this.getAttribute('index');
             console.log(index);
             for (var k = 0; k < lists.length; k++){
                 lists[k].style.display = 'none';
             }
             lists[index].style.display = 'block';
            //  获取数据
            get();
        }       
    }
}
get_course();

